package com.kuang.controller;

import com.kuang.bean.Books;
import com.kuang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //controller 调service
    @Autowired
//    @Qualifier("BookServiceImpl")
    private BookService bookService;
    //查询所有的书籍并返回
    @RequestMapping("/allBook")
    public String list(Model model){
//      Books books = bookService.queryBookById(1);
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }
    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }
    //添加书籍的请求
    @RequestMapping("/addBook")
    public String AddBook(Books books){
        System.out.println(books.getBookCounts()+books.getBookName());
        System.out.println("addBook=>"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    //跳转到修改书籍页面
    @RequestMapping("/toupdate")
    public String toUpdateBook(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBook",books);
        return "updateBook";
    }
    @RequestMapping("updateBook")
    public String updateBook(Books books){
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    //删除按钮
    @RequestMapping("/delBook/{bookID}")
    public String delBook(@PathVariable("bookID") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    //查询书籍by ID
    @RequestMapping("/querybyID")
    public String querybyID(int id,Model model){
        Books books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("qqbooks",books);
        return "queryBook";
    }
}
